package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"net/http"
	"net/url"
	"new_club_recruitment/global"
	"new_club_recruitment/model/common/response"
	"new_club_recruitment/model/user"
	"new_club_recruitment/model/user/request"
	"time"
)

type ApiUser struct {
}

//AddUsers 添加用户信息
func (a *ApiUser) AddUsers(c *gin.Context) {
	//1.解析参数
	var info user.SysUser
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMessage("添加失败", c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if err := userService.AddUserInfo(info); err != nil {
		global.GVA_LOG.Error("添加失败!", zap.Error(err))
		response.FailWithMessage("添加失败", c)
	} else {
		//3.返回响应
		response.OkWithDetailed(nil, "添加成功", c)
	}
}

// GetUsers 获得用户列表
func (a *ApiUser) GetUsers(c *gin.Context) {
	var pageInfo request.UserSearch
	//绑定前端数据
	if err := c.ShouldBindQuery(&pageInfo); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMessage("获取失败", c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if list, total, err := userService.GetUserInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//3.返回响应
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.PageNum,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (a *ApiUser) UpdateUser(c *gin.Context) {
	//1.解析参数
	var info user.SysUser
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if err := userService.UpdateUserInfoList(info); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.Ok(c)
	}
}

//GetExaUser 获取单一报名人员信息
func (a *ApiUser) GetExaUser(c *gin.Context) {
	id := c.Param("id")
	//业务处理
	data, err := userService.GetUserInfo(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.Fail(c)
	} else {
		response.OkWithData(data, c)
	}
}

// ExportExcel 导出Excel文件信息
func (a *ApiUser) ExportExcel(c *gin.Context) {
	var excelInfo []user.SysUser
	_ = c.ShouldBindJSON(&excelInfo)
	file, err := userService.ParseInfoList1Excel(excelInfo)
	if err != nil {
		global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
		response.FailWithMessage("转换Excel失败", c)
		return
	}
	fileName := fmt.Sprintf("%s.xlsx", "用户信息")
	fileName = url.QueryEscape(fileName)
	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
	c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	http.ServeContent(c.Writer, c.Request, fileName, time.Now(), file)
}
